class GUI {

  private int width;
  private int height;
  private char menu;
  private int levelOfCom;
  private boolean autoComplete; 
  ArrayList<Button> buttons = new ArrayList<Button>() ;
  ArrayList<Field> fd;

  private color clRed = #ff0000;
  private color clButton = #9ca6b0;
  private color clBlue = #0000ff;
  private color clGrey = #cccccc;
  private color clWhite = #FFFFFF;       
  private color clBlack = #000000;       
  private color clGreen = #00FF00;       
  private boolean startBattle=false;

  private byte difficulty = 1;
  private boolean auto = false;

  Controller controller1;
  Controller controller2;
 


  GUI(int w, int h) {
    width = w;
    height = h;
    menu = '1';
    setLevel(0);
    setAutoComplete(true);
  }
  public void setLevel(int a) {
    this.levelOfCom=a;
  }
  public int getLevel() {
    return this.levelOfCom;
  }
  public boolean getLevel(int i) {
    boolean b=false;
    if (i == getLevel()) {
      return true;
    }
    else { 
      return false;
    }
  }
  public void setAutoComplete(boolean b) {
    this.autoComplete=b;
  }
  public boolean getAutoComplete() {
    return this.autoComplete;
  }

  public void contur(Field field) {
    for (int i = 0; i < field.getCount(); i++) {
      for (int j = 0; j < field.getCount(); j++) {  
        fill(clGrey);
        rect(i*field.getCellSize() + field.getCorn().getX(), j*field.getCellSize() + field.getCorn().getY(), field.getCellSize(), field.getCellSize());
        if (field.getCellValue(i, j) == -2) {
          fill(clBlue);
        }
        if (field.getCellValue(i, j) > 0 && field.getCellValue(i, j)<11) {
          fill(clGreen);
        } 
        if (field.getCellValue(i, j) == 11) {
          fill(clRed);
        }
        if (field.getCellValue(i, j) == 12) {
          fill(clGrey);
        }
        if (field.getCellValue(i, j) == 0) {
          fill(clWhite);
        }
        rect(i*field.getCellSize() + field.getCorn().getX() + 2, j*field.getCellSize() + field.getCorn().getY() + 2, field.getCellSize() - 4, field.getCellSize() - 4);
        fill(clBlack);
       // text(field.getCellValue(i, j), i*field.getCellSize() + 12 + field.getCorn().getX(), j*field.getCellSize() + 20 + field.getCorn().getY());
      }
    }
  }

  public void highlight(TPoint point1, Controller  c, int i) {
    Field field=c.getField(i);
    TPoint pointLight = new TPoint(0, 0);
    pointLight = c.cell(point1, field);
    if (pointLight.getX() != -1 && pointLight.getY() != -1 && pointLight.getY() != 10 && pointLight.getY() != 10) { 
      if (field.getCellValue(pointLight.getY(), pointLight.getX()) == 0) {   
        contur(field);
        ////////////////////////  
        fill(clRed);
        rect(pointLight.getY()*field.getCellSize() + field.getCorn().getX(), pointLight.getX()*field.getCellSize() + field.getCorn().getY(), field.getCellSize(), field.getCellSize());
        fill(clWhite);
        rect(pointLight.getY()*field.getCellSize() + field.getCorn().getX() + 2, pointLight.getX()*field.getCellSize() + field.getCorn().getY() + 2, field.getCellSize() - 4, field.getCellSize() - 4);
        fill(clBlack);
        text("+", pointLight.getY()*field.getCellSize() + 12 + field.getCorn().getX(), pointLight.getX()*field.getCellSize() + 20 + field.getCorn().getY());
      }
    }
    else {
      contur(field);
    }
  }

  public void mainMenu() {

    buttons.clear();
    background(clGrey);

    fill(clRed);
    textSize(floor(width / 9));
    textAlign(CENTER, CENTER);
    text("Морской бой", floor(width / 2), floor(height / 3));

    Button bStart = new Button(floor(0.1 * width), floor((15 * height) / 20), floor(0.2 * width), floor(0.1 * height), "Начать", '2', clRed, clButton, 1, false);
    Button bSettings = new Button(floor(0.4 * width), floor((15 * height) / 20), floor(0.2 * width), floor(0.1 * height), "Настройки", '3', clRed, clButton, 1, false);
    Button bExit = new Button(floor(0.7 * width), floor((15 * height) / 20), floor(0.2 * width), floor(0.1 * height), "Выход", '4', clRed, clButton, 1, false);

    buttons.add(bStart);
    buttons.add(bSettings);
    buttons.add(bExit);
  }


  public int getWidth() {
    return width;
  }

  public int getHeight() {
    return height;
  }

  private void drawButtons() {
    for (int i = 0 ; i < buttons.size() ; i++) {
      Button button = buttons.get(i);
      fill(button.getButtonColor());
      rect(button.getX(), button.getY(), button.getWidth(), button.getHeight());
      fill(button.getTextColor());
      textSize(floor(button.getWidth()/8));
      textAlign(CENTER, CENTER);
      text(button.getText(), button.getX() + floor(button.getWidth() / 2), button.getY() + floor(button.getHeight() / 2));
    }
  } 

  private void workMenu(int x, int y) {    
    for (int i = 0 ; i < buttons.size() ; i++) {
      Button button = buttons.get(i);
      if (x > button.getX() && x < button.getX()+ button.getWidth() && y > button.getY() && y < button.getY() + button.getHeight()) {
        buttons.get(i).setTextColor(clBlue);
      } 
      else {
        buttons.get(i).setTextColor(clRed);
      }
    }
    drawButtons();
  }
  private void highlightButton(int x, int y) {

    for (int i = 0 ; i < buttons.size() ; i++) {

      int x1 = buttons.get(i).getX();
      int y1 = buttons.get(i).getY();
      int w = buttons.get(i).getWidth();
      int h = buttons.get(i).getHeight();

      if (buttons.get(i).getType() == 1) {
        if (x > x1 && x < x1 + w && y > y1  && y < y1 + h) {
          buttons.get(i).drawHighlightButton();
        }
        else {
          buttons.get(i).drawButton();
        }
      }
      else {

        if (x > x1 - w/2 && x < x1 + w/2 + width/3.5 && y > y1 - h/2 && y < y1 + h/2) {
          //background(clGrey);
          if (!buttons.get(i).isChecked()) {
            buttons.get(i).drawHighlightButton();
          }
        }
        else {
          buttons.get(i).drawButton();
        }
      }
    }
  }

  private void settings() {
    buttons.clear();
    background(clGrey);
    textAlign(CENTER, CENTER);
    textSize(floor(width / 15)); 
    text("Настройки", floor(width / 2), floor(height / 10));
    textSize(floor(width / 25)); 
    text("Уровень сложности", floor(width / 4), floor(height / 4.5));
    text("Автозаполнение", floor(width / 4.5), floor(height / 1.55));

    Button bEasy = new Button(floor(width * 0.08), floor(height * 0.35), floor(width * 0.05), floor(width * 0.05), "Легкий", '1', clRed, clButton, 2, getLevel(0));
    Button bNormal = new Button(floor(width * 0.08), floor(height * 0.45), floor(width * 0.05), floor(width * 0.05), "Нормальный", '2', clRed, clButton, 2, getLevel(1));
    //Button bNightmare = new Button(floor(width * 0.08), floor(height * 0.55), floor(width * 0.05), floor(width * 0.05), "//бедишь", '3', clRed, clButton, 2, getLevel(2));
    Button back = new Button(floor(width * 0.08), floor(height * 0.85), floor(0.2 * width), floor(0.1 * height), "Назад", '2', clRed, clButton, 1, false);
    Button auto = new Button(floor(width * 0.08), floor(height * 0.75), floor(width * 0.05), floor(width * 0.05), "Автозаполнение", '4', clRed, clButton, 3, getAutoComplete());
    //Button bNightmare = new Button(floor(width * 0.08), floor(height * 0.55), floor(width * 0.05), floor(width * 0.05), "Хрен победишь", '3', clRed, clButton);
    buttons.add(bEasy);
    buttons.add(bNormal);
   // buttons.add(bNightmare);
    buttons.add(back);
    buttons.add(auto);
  }

  private void battleMenu() {
    buttons.clear();
    background(clGrey);
    textSize(floor(width / 50));
    textAlign(0);
    fd = new  ArrayList<Field>();
    fd.add(new Field(10, 10));
    fd.add(new Field(10, 10));
    controller1 = new Controller(fd, false);
    if (getAutoComplete()) { 
      controller1.generation(controller1.getField(1));
     // controller1.syncField(true);
      controller1.getField(1).removeNegOne();
      contur(controller1.getField(1));
    }
    
    contur(fd.get(1));
    fd = new  ArrayList<Field>();
    fd.add(new Field(400, 10));
    fd.add(new Field(400, 10));
    controller2 = new Controller(fd, true);
    contur(fd.get(0));
    //menu=2;
    Button bStart = new Button(floor(width * 0.7), floor(height * 0.03), floor(0.2 * width), floor(0.1 * height), "Начать битву", '2', clRed, clButton, 1, false);
    Button bGeneration = new Button(floor(width * 0.7), floor(height * 0.15), floor(0.2 * width), floor(0.1 * height), "Главное меню", '2', clRed, clButton, 1, false);
    buttons.removeAll(buttons);
    buttons.add(bStart);
    buttons.add(bGeneration);
  }

  private void changeAll(int n) {
    for (int i = 0 ; i < buttons.size() ; i++) {
      if (buttons.get(i).getType() == 2) { 
        if (i == n) {
          buttons.get(i).setChecked(true); 
          buttons.get(i).drawButton();
        }
        else {
          buttons.get(i).setChecked(false);
          buttons.get(i).drawButton();
        }
      }
    }
  }
  private boolean checkPosUser() {
    if (controller1.getShips().size() == 10) {
      return true;
    } 
    else {
      return false;
    }
  }
  private void performAction(int x, int y) {
    for (int i = 0 ; i < buttons.size() ; i++) {
      int x1 = buttons.get(i).getX();
      int y1 = buttons.get(i).getY();
      int w = buttons.get(i).getWidth();
      int h = buttons.get(i).getHeight();

      if (x > x1 && x < x1 + w  && y > y1 && y < y1 + h) {
        if (buttons.get(i).getText().equals("Начать битву") && checkPosUser()) {
          startBattle = true;
          buttons.clear();
          background(clGrey);
          if (getLevel () == 1){
          controller2.genHard(controller2.getField(1));}
          else {controller2.generation(controller2.getField(1));}
          controller1.getField(1).removeNegOne();
        } 
        else
        {
          menu='1';
          startBattle = false;
          mainMenu();
        }
      }
    }
  }
  private void changeButton(int x, int y) {
    for (int i = 0 ; i < buttons.size() ; i++) {
      int x1 = buttons.get(i).getX();
      int y1 = buttons.get(i).getY();
      int w = buttons.get(i).getWidth();
      int h = buttons.get(i).getHeight();
      boolean c = buttons.get(i).isChecked();

      if (x > x1 - w/2 && x < x1 + w/2 + width/3.5 && y > y1 - h/2 && y < y1 + h/2) {
        if (buttons.get(i).getType() == 3) {
          if (c) {
            buttons.get(i).setChecked(false);
            buttons.get(i).drawButton();
            setAutoComplete(false);
          } 
          else {
            buttons.get(i).setChecked(true);
            buttons.get(i).drawButton();
            setAutoComplete(true);
          }
        }
        else if (buttons.get(i).getType() == 2) {
          changeAll(i);
          setLevel(i);
        }
        else if (buttons.get(i).getType() == 1) {
          menu='1';
          mainMenu();
        }
      }
    }
  }


  private void changeMenu(int x, int y) {
    char oldMenu = menu;
    for (int i = 0 ; i < buttons.size() ; i++) {
      Button button = buttons.get(i);
      if (x > button.getX() && x < button.getX()+ button.getWidth() && y > button.getY() && y < button.getY() + button.getHeight()) {
        menu = button.getAction();
      }
    }
    if (oldMenu != menu) {
      switch (menu) {
      case '1': 

        break;
      case '2':
        battleMenu();   
        break;
      case '3': 
        settings();
        break;
      case '4': 
        exit();
        break;
      }
    }
  }

  private void clickField(int x, int y) {
    int w=controller2.getField(0).getCellSize()*controller2.getField(0).getCount();
    int h=controller2.getField(0).getCellSize()*controller2.getField(0).getCount();
    int x1=controller2.getField(0).getCorn().getX();
    int y1=controller2.getField(0).getCorn().getY();

    if (x>x1 && x<x1+w && y>y1 && y<y1+h) {
      if (controller2.isTurn()) {
        
        controller2.makeShoot(new TPoint(x, y), true);
        contur(controller2.getField(0));
      }
      
    if (!controller2.isTurn()){
      controller1.setTurn(true);
    } 
   while (controller1.isTurn()){
           
            TPoint p1= controller1.getShoot();
            controller1.makeShoot(p1,false);  
            controller1.syncField(p1);
            contur(controller1.getField(0));
      }
   if (!controller1.isTurn()){
      controller2.setTurn(true);
    } 
    if(controller1.win()){
     menu=4;
     background(200);
     fill(clRed);
    textSize(floor(width / 9));
    textAlign(CENTER, CENTER);
    text("Вы проиграли!", floor(width / 2), floor(height / 3));
   // Button bSettings = new Button(floor(0.4 * width), floor((15 * height) / 20), floor(0.2 * width), floor(0.1 * height), "Начать заново", '3', clRed, clButton, 1, false);
   
     
    }
    if(controller2.win()){
      menu=4;
     background(200);
     fill(clRed);
    textSize(floor(width / 9));
    textAlign(CENTER, CENTER);
    text("Вы победили!", floor(width / 2), floor(height / 3));
    }
 // println(controller2.getMaxLengthLiveShip());    
      
    }
  }
  private void click(int x, int y) {
    switch (menu) {
    case '1': 
      changeMenu(x, y);
      break;
    case '2':
      performAction(x, y);
      if (startBattle) {
        clickField(x, y);
      }
      break;
    case '3': 
      changeButton(x, y);
      //changeCheckButton(x, y);
      break;
    case '4': 
        println("SSSSSSS");
      break;
    }
  }

  public void draw(int x, int y) {
    switch (menu) {
    case '1': 
      workMenu(x, y);
      break;
    case '2': 
      highlight(new TPoint(x, y), controller1, 1);
      highlight(new TPoint(x, y), controller2, 0);
      highlightButton(x, y);
      break;
    case '3':
      highlightButton(x, y); 
      break;
    case '4': 

      break;
    }
  }
  public void mReleased(TPoint start, TPoint stop) {
    switch (menu) {
    case '1': 

      break;
    case '2':
      if (!getAutoComplete()) { 
        controller1.createShip(start, stop, false, controller1.getField(1));
        //controller1.syncField(true);
        contur(controller1.getField(1));
      }    
      break;
    case '3': 

      break;
    case '4': 
      break;
    }
  }
}

